package com.moneywelcome.userserviceassembly.util;

import org.springframework.beans.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

public class MyBeanUtils {
    public static <T> T copyProperties(Object obj,Class<T> type)  {
        T t = null;
        try {
            t = type.getConstructor().newInstance();
            BeanUtils.copyProperties(obj,t);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return t;
    }

    public static <T> List<T> copyList(Collection<?> objs, Class<T> type)  {
        List<T> lists = new ArrayList<>();
        T t = null;
        try {
            for (Object obj : objs){
                 t = type.getConstructor().newInstance();
                BeanUtils.copyProperties(obj, t);
                lists.add(t);
            }


        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return lists;
    }

    public static <T> Set<T> copySet(Collection<?> objs, Class<T> type)  {
        Set<T> sets = new HashSet<>();
        T t = null;
        try {
            for (Object obj : objs){
                t = type.getConstructor().newInstance();
                BeanUtils.copyProperties(obj, t);
                sets.add(t);

            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return sets;
    }

}
